<template>
  <Panel>
    <template #title>
      <p>重置密码</p>
    </template>
    <template #content>
      <div>
        <el-form
          :model="passForm"
          status-icon
          :rules="rules"
          ref="passForm"
          label-width="100px"
          class="demo-passForm"
          size="mini"
        >
          <el-form-item label="原密码" prop="oldPass">
            <el-input
              type="text"
              v-model="passForm.oldPass"
              autocomplete="off"
              @blur="checkPass()"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="pwd">
            <el-input type="text" v-model="passForm.pwd" autocomplete="off">
            </el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass">
            <el-input
              type="text"
              v-model="passForm.checkPass"
              autocomplete="off"
            >
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('passForm')"
              >提交</el-button
            >
            <el-button @click="resetForm('passForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </template>
  </Panel>
</template>

<script>
import { validatePwd, checkPass, recheckPwd } from '../../utils/validate'
import { checkOldPass, editPass } from '../../api/user.js'
import local from '../../utils/local.js'

export default {
  data() {
    return {
      flag: false,
      OldPassStatus: false,
      passForm: {
        oldPass: '',
        pwd: '',
        checkPass: ''
      },
      rules: {
        oldPass: [{ validator: validatePwd, trigger: 'blur' }],
        pwd: [{ validator: recheckPwd(this), trigger: 'blur' }],
        checkPass: [{ validator: checkPass(this), trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          if (this.OldPassStatus) {
            const data = await editPass({
              newPwd: this.passForm.pwd
            })
            this.$message({
              message: data.msg,
              type: 'success'
            })
            local.del('token')
            this.$router.push('/login')
          } else {
            this.$message({
              message: '原密码错误，再想想吧',
              type: 'success'
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    async checkPass() {
      const data = await checkOldPass({
        oldPwd: this.passForm.oldPass
      })
      if (data.code === '00') {
        this.$message({
          message: data.msg,
          type: 'success'
        })
        this.OldPassStatus = true
      } else {
        this.$message({
          message: data.msg,
          type: 'error'
        })
        this.OldPassStatus = false
      }
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.el-form {
  width: 50%;
}
</style>
